<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/5/23
 * Time: 9:19
 */

namespace App\Admin\Controllers;

use App;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Tab;
use Encore\Admin\Widgets\Chart\Pie;
use Encore\Admin\Widgets\Form;
use DecreaseUserInfoHelper;
use DecreaseSelectHelper;
use App\Admin\Extensions\timeLine;
use App\Admin\Extensions\tileimg;
class DecreaseUserInfoController
{

    use ModelForm;

    public function index(Request $request,$id=0,$uid=0,$tid,$appid,$time="")
    {
        $time = empty($time) ? date("Y-m-d",time()) : date("Y-m-d",strtotime($time));
        if($request->input('time')){
            $time = $request->input('time');
        }
        $appid = empty($appid) ? "" : base64_decode($appid);
        $baseCircleData = DecreaseUserInfoHelper::baseUserCircleData($id,$time);

        return Admin::content(function (Content $content) use($id,$uid,$tid,$time,$appid,$baseCircleData){
            $content->header('用户详情');
            $content->row(function (Row $row)use($id,$uid,$tid,$time,$appid,$baseCircleData) {
                $row->column(12, function (Column $column)use($id,$uid,$tid,$time,$appid,$baseCircleData) {

                    $tab = new Tab();
                    $userInfo = $this->userInfo($id,$tid,$time,$uid,$appid);
                    $signIn = $this->signIn($id,$tid,$time,$appid,$baseCircleData);
                    $timeLine = $this->timeLine($uid);
                    $tab->add('用户信息', $userInfo);
                    $tab->add('用户打卡', $signIn);
                    $tab->add('体重趋势', $timeLine);
                    $tab->add('周期管理', $timeLine);

                    $column->append((new Box('用户详情（'.$time.'）', $tab))->style('info')->solid());
                });
            });
        });
    }



    public function userInfo($id,$tid,$time,$uid,$appid){
        $box = "";
        if(!empty($uid)){
            $recode = DecreaseUserInfoHelper::getRecode($id);
            if($recode){
                $datainfo = DecreaseUserInfoHelper::userInfo($id,$tid,$time);
                $box .= new Box(' ', $datainfo);
            }
        }
        if(!empty($id)){
            $dataRrcode =  DecreaseUserInfoHelper::recode($id,$tid);
            $box .= new Box('答题记录('.$dataRrcode['time'].')',$dataRrcode['html']);
            $middle = DecreaseUserInfoHelper::middle($id,$tid);
            $box .=  new Box('中间值及减脂方案('.$middle['time'].')', $middle['html']);
        }
        $matchStauts = DecreaseUserInfoHelper::matchStauts($uid,$tid);
        if($matchStauts){
          $orderInfo =  DecreaseSelectHelper::orderInfo($matchStauts->oid,base64_encode($appid));
            $box .= new Box('订单信息', $orderInfo);
        }
        return $box;
    }


    //时间轴
    public function timeLine($uid){
        $box = "";
        $timeline = new timeLine(
            [
                ['comments', "第二周完成",[],"2017-05-12 12:00:00"],
                ['comments', "第一周完成",['发送第一周反馈链接','发送第二周方案','轻断食用户提醒'],"2017-05-12 12:00:00"],

            ]
        );
        $box .= new Box('zhouqi',$timeline);
        return $box;
    }


    public function signIn($id,$tid,$time,$appid,$baseCircleData){
        $box = "";
        $userCircle = DecreaseUserInfoHelper::userCircle($id,$tid,$time,$appid,$baseCircleData);
        $form = new Form();
        $url = $userCircle['url'];
        $form->action($url);
        $form->date('time',"日期");
        //$doughnut = new Carousel($data['imgArr']);
        //$img = "<div style='height: 70%;width: 70%; padding: 0 20%;'><div>$doughnut</div></div>";
        if($userCircle['imgSportArr'] && $userCircle['imgFoodArr']){
            $imgFood = new tileimg($userCircle['imgFoodArr']);
            $imgSport = new tileimg($userCircle['imgSportArr']);
            $rows = [
                [$form],
                [$userCircle['sport']],
                [$imgSport],
                [$userCircle['food']],
                [$imgFood],
                [$userCircle['evaluate']],
            ];
        }elseif($userCircle['imgSportArr'] && !$userCircle['imgFoodArr']){
            $imgSport = new tileimg($userCircle['imgSportArr']);
            $rows = [
                [$form],
                [$userCircle['sport']],
                [$imgSport],
                [$userCircle['food']],
                [],
                [$userCircle['evaluate']],
            ];
        }elseif(!$userCircle['imgSportArr'] && $userCircle['imgFoodArr']){
            $imgFood = new tileimg($userCircle['imgFoodArr']);
            $rows = [
                [$form],
                [$userCircle['sport']],
                [],
                [$userCircle['food']],
                [$imgFood],
                [$userCircle['evaluate']],
            ];
        }else{
            $rows = [
                [$form],
                [$userCircle['sport']],
                [],
                [$userCircle['food']],
                [],
                [$userCircle['evaluate']],
            ];
        }
        $tab = new Table([''], $rows);
        $box .= new Box('用户打卡信息及评价信息', $tab);
        $evaluate = DecreaseUserInfoHelper::evaluate($id,$tid,$time,$appid,$baseCircleData);
        $box .= new Box('用户打卡信息及评价信息', $evaluate);
        return $box;
    }


    /**打卡统计图
     * @param Request $request
     * @param $appid
     * @param string $time
     * @return mixed
     */
   public function dataStats(Request $request,$appid,$time=""){
        $time = empty($time) ? date("Y-m-d",time()) : date("Y-m-d",strtotime($time));
        if($request->input('time')){
            $time = $request->input('time');
        }
       $appid = empty($appid) ? "" : base64_decode($appid);
        return Admin::content(function (Content $content)use($appid,$time){
            $content->row(function (Row $row)use($appid,$time){
                $data = DecreaseUserInfoHelper::stats($appid,$time);
                $row->column(8, function (Column $column)use($appid,$time,$data){
                    $form = new Form();
                    $url = $data['posturl'];
                    $form->action($url);
                    $form->date('time',"日期");

                    $pie_food = new Pie([
                        ['已打卡', $data['food_num'][0]],
                        ['未打卡', $data['food_num'][1]]
                    ]);
                    $box_food = new Box('饮食打卡',$pie_food);

                    $pie_sport = new Pie([
                        ['已打卡', $data['sport_num'][0]],
                        ['未打卡', $data['sport_num'][1]]
                    ]);
                    $box_sport= new Box('运动打卡',$pie_sport);

                    $rows = [
                        [$form],
                        [$box_food],
                        ['<div><a href="'.$data['food_moreurl'].'">点我查看饮食打卡详情</a></div> '],

                        [$box_sport],
                        ['<div><a href="'.$data['sport_moreurl'].'">点我查看运动打卡详情</a></div> '],
                    ];
                    $tab = new Table([''], $rows);
                    $column->append((new Box('打卡统计（'.$time.'）',$tab))->style('info')->solid());
                });
            });
        });
    }


    //打卡详情
    public function statsInfo($appid,$type,$time){
        $appid = empty($appid) ? "" : base64_decode($appid);
        return Admin::content(function (Content $content)use($appid,$type,$time){
            $content->row(function (Row $row)use($appid,$type,$time){
                $row->column(12, function (Column $column)use($appid,$type,$time){
                    $data = DecreaseUserInfoHelper::statsInfo($appid,$type,$time);
                    if($type == "food"){
                        $tittle = "饮食";
                        $have_rows =$data['have_food'];
                        $haveNot_rows = $data['haveNot_food'];
                    }elseif($type == "sport"){
                        $tittle = "运动";
                        $have_rows = $data['have_sport'];
                        $haveNot_rows = $data['haveNot_sport'];
                    }
                    $headers = ['Id', 'nick_name'];
                    $tab = new Tab();
                    $tab->add('已打卡', new Table($headers,$have_rows));
                    $tab->add('未打卡', new Table($headers,$haveNot_rows));
                    $column->append((new Box($tittle.'打卡统计（'.$time.'）',$tab))->style('info')->solid());
                });
            });
        });
    }



    //用户信息备注
    public function remark(Request $request){
        return Admin::content(function (Content $content) use($request){
            $content->row(function (Row $row)use($request){
                $row->column(12, function (Column $column)use($request){
                    $rid = $request->input('rid');
                    $uid = $request->input('uid');
                    $tid = $request->input('tid');
                    $appid =$request->input('appid');
                    $time =$request->input('time');
                    $data = DecreaseUserInfoHelper::remarkData($rid,$uid,$tid,$appid,$time);
                    $form = new Form($data);
                    $form->action('/admin/testing/decrease/users/storeremark');
                    $form->method('POST');
                    $form->hidden('tid');
                    $form->hidden('rid');
                    $form->hidden('uid');
                    $form->hidden('url');
                    $form->textarea('remark',"用户信息备注");
                    $column->append((new Box('编辑用户信息备注',$form))->style('info'));
                });
            });
        });
    }

    //存储用户备注信息
    public function storeRemark(Request $request){
       $data=$request->input();
        if($data['uid'] && $data['rid'] && $data['tid']){
            $res['uid']=$data['uid'];
            $res['rid']=$data['rid'];
            $res['tid']=$data['tid'];
            $res['remark']=$data['remark'];
            DecreaseUserInfoHelper::storeRemark($res);
        }
        redirectToUrl($data['url']);
    }




}